<?php

declare(strict_type=1);

namespace Service\FeedBackService;

use App\Model\BbcModel\Suggest;
use Service\BaseService;
use Service\Code\Normal;

class FeedBackService extends BaseService
{
    /**
     * 
     * 意见反馈
     * @param string $title
     * @param string $content
     * @param string $pic
     * @param string $mobile
     * @param string $email
     * @param integer $mn_sid
     * @param integer $mid
     * @return void
     */
    public function feedBack(string $title, string $content, string $pic, string $mobile, string $email, int $mn_sid, int $mid)
    {
        if (empty($title) || empty($title)) {
            return Normal::returnCode(Normal::FAIL, '标题或者内容不能为空');
        }
        if (empty($mobile)) {
            return Normal::returnCode(Normal::FAIL, '手机号码必填');
        }
        if (!preg_match("/^1[34578]\d{9}$/", $mobile)) {
            return Normal::returnCode(Normal::FAIL, '手机号码格式不对');
        }
        if (mb_strlen($title) > 20) {
            return Normal::returnCode(Normal::FAIL, '标题不能超过20个字');
        }
        if (mb_strlen($content) > 200) {
            return Normal::returnCode(Normal::FAIL, '内容不能超过200个字');
        }
        if (!empty($pic)) {
            $num = count(explode(',', $pic));
            if ($num > 6) {
                return Normal::returnCode(Normal::FAIL, '图片不超过六张');
            }
        }
        Suggest::instance()->insert([
            'title' => trim($title),
            'content' => trim($content),
            'mobile' => trim($mobile),
            'email' => trim($email),
            'pic' => $pic,
            'mn_sid' => $mn_sid,
            'mid' => $mid,
            'sid' => getSID(),
            'cd' => time()
        ]);
        return Normal::returnCode(Normal::SUC, '反馈成功');
    }
}
